<?php

namespace App\Http\Controllers\Backend1;


use App\Http\Controllers\Backend1\BaseController;

use App\Services\Company\Models\CompanyOrderGoods;
use App\Services\Company\Interfaces\CompanyRoleServiceInterface;
use App\Services\Company\Models\CompanyRole;
use App\Services\Company\Services\CompanyRoleService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Services\Company\Models\Company;
use App\Services\Company\Interfaces\CompanyOrderServiceInterface;
use App\Services\Company\Services\CompanyOrderService;



class CompanyController extends BaseController
{

    /**
     *
     * 店铺列表
     * @param Request $request
     * @return JsonResponse
     */
    public  function  index(Request $request)
    {

        $keywords = $request->input('keywords', '');
        $isExpired = $request->input('is_expired', 0);
        $roleId = $request->input('role_id', 0);
        $sort = $request->input('sort', 'created_at');
        $order = $request->input('order', 'desc');

        $companies = Company::query()
            ->with(['role:id,title'])
            ->when($keywords, function ($query) use ($keywords) {
                 $query->where('name', 'like', '%' . $keywords . '%')
                            ->orWhere('uuid', $keywords);
            })
            ->when($isExpired, function ($query) use ($isExpired) {
                $query->where('expired_at', $isExpired == 1 ? '<' : '>', Carbon::now());

            })
            ->when($roleId, function ($query) use ($roleId) {
                $query->where('role_id', 'role_id');
            })
            ->orderBy($sort, $order)
            ->paginate($request->input('size', 10));

        $roles = CompanyRole::query()->select(['id', 'title'])->get();
        return $this->successData(compact('companies','roles' ));

    }



    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function createSubscribe(Request $request): JsonResponse
    {
        $roleId = $request->input('role_id');
        $expiredId =$request->input('expired_id');
        $companyId = $request->input('company_id');

        if (!$expiredId|| !$roleId || !$companyId) {
            return $this->error(__('参数错误'));
        }
        /**
         * @var CompanyRoleService $companyRoleService
         *
         * */
        $companyRoleService = app()->make(CompanyRoleServiceInterface::class);

        $company  = Company::query()->with(['role'])->where('id',$companyId)->first();
        $role = $company['role']??'';
        if (!$role) {
            $companyRoleService->CompanyJoinRole($companyId, $roleId,$expiredId);

        }else{
            $companyRoleService->CompanyContinueRole($companyId,$role,$roleId,$expiredId );
        }
        return $this->success(__('权限添加成功'));
    }

    public function deleteSubscribe(Request $request)
    {
        $companyId = $request->input('company_id');
        $company = Company::query()->with(['role'])->where('id', $companyId)->first();
        $role = $company['role']??null;
        if (!$role) {
            return $this->error(__('不是会员或过期'));
        }
        /**
         * @var CompanyRoleService $companyRoleService
         *
         **/
        $companyRoleService = app()->make(CompanyRoleServiceInterface::class);
        $companyRoleService->CompanyDeleteRole($companyId, $company['role_id']);
        return $this->success(__('取消会员权限成功'));


    }










}